package cn.toolck.my.spring.service.impl;

import cn.toolck.my.spring.annotation.Autowired;
import cn.toolck.my.spring.annotation.Component;
import cn.toolck.my.spring.annotation.Service;
import cn.toolck.my.spring.annotation.Transactional;
import cn.toolck.my.spring.dao.AccountDao;
import cn.toolck.my.spring.pojo.Account;
import cn.toolck.my.spring.service.TransferService;

@Service("transferService")
public class TransferServiceImpl implements TransferService {

    @Autowired
    private AccountDao accountDao;

    @Override
    @Transactional(value = "myTransactionalManager", rollbackFor = Exception.class)
    public void transfer(String fromCardNo, String toCardNo, int money) throws Exception {

        Account from = accountDao.queryAccountByCardNo(fromCardNo);
        Account to = accountDao.queryAccountByCardNo(toCardNo);

        from.setMoney(from.getMoney() - money);
        to.setMoney(to.getMoney() + money);

        accountDao.updateAccountByCardNo(to);
        int i = 1 / 0;
        accountDao.updateAccountByCardNo(from);

    }

    public AccountDao getAccountDao() {
        return accountDao;
    }

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }
}
